<?php

class Core_Model_Article_Content extends Dcmp_Model_Tag {

    private static $pagebreak = '/<div style="page-break-after: always;">\s*<span style="display: none;">&nbsp;<\/span><\/div>/i';

    public function __construct() {
        parent::__construct();
        $this->tagType = 'article';
    }

    protected function afterInsert($transaction) {
        if ($this->issetData('sef_url')) {
            App::getHelper('website')->addSefUrl('article_content', $this->getId(), '/article/content/content/id/' . $this->getId(), '/article/' . $this->getSefUrl());
        }
        parent::afterInsert($transaction);
    }

    protected function afterUpdate($transaction) {
        if ($this->propertyIsChanged('sef_url')) {
            App::getHelper('website')->deleteSefUrl('article_content', $this->getId());
            if ($this->issetData('sef_url')) {
                App::getHelper('website')->addSefUrl('article_content', $this->getId(), '/article/content/content/id/' . $this->getId(), '/article/' . $this->getSefUrl());
            }
        }
        parent::afterUpdate($transaction);
    }

    public function updateClickCount($id) {
        Kd_Data_Factory::getUpdateDcl()
                ->into('{t}article_content')
                ->value('click_count', Kd_Data_Dcl_Fn::addition('click_count', 1))
                ->where()->eq('id', ':id:i')->closest()
                ->addParameter('id', $id)
                ->execute();
        return $this;
    }

    public function getFirstPicture() {
        if ($this->issetData('picture')) {
            $result = explode(',', $this->getPicture());
            return $result[0];
        }
        return '';
    }

    public function getPagebreakCount(){
        return count(preg_split(self::$pagebreak, $this->getContent()));
    }

    public function getPagebreakContent($index){
        $result = preg_split(self::$pagebreak, $this->getContent());
        if(count($result) < $index){
            $index = count($result) - 1;
        }
        return $result[$index];
    }

    public function getCategory() {
        if ($this->issetData('category_id')) {
            $this->data['category'] = App::getModel('article/category')->load($this->getCategoryId());
        }
        return $this->data['category'];
    }

    

    protected function initializeTable($table) {
        $table->setName('{t}article_content');
        $table->createColumn('id', 'integer', TRUE, TRUE);
        $table->createColumn('title', 'string');
        $table->createColumn('content', 'string');
        $table->createColumn('sef_url', 'string');
        $table->createColumn('category_id', 'integer');
        $table->createColumn('resource', 'string');
        $table->createColumn('click_count', 'integer')->setDefaultValue(0);
        $table->createColumn('author', 'string');
        $table->createColumn('end_time', 'datetime');
        $table->createColumn('start_time', 'datetime')->setDefaultValue(date('Y-m-d H:i:s'));
        $table->createColumn('is_hot', 'integer')->setDefaultValue(0);
        $table->createColumn('is_new', 'integer')->setDefaultValue(0);
        $table->createColumn('is_top', 'integer')->setDefaultValue(0);
        $table->createColumn('picture', 'string');
        $table->createColumn('introduction', 'string');
        $table->createColumn('owner_id', 'integer');
        parent::initializeTable($table);
    }

}